$(function() {
    $.ajax({
        type: 'get',
        url: 'http://localhost:8080/api/v1/index/category',
        //   data: {},
        success: (backData) => {
            // console.log(backData);
            let categoryRes = template('categoryTemp', backData);
            let categoryMenuRes = template('categoryMenuTemp', backData)
            $('ul.level_two').html(categoryRes)
            $('ul.left_menu').html(categoryMenuRes)
        },
    });

    let id = new URLSearchParams(location.search)
    id = id.get('id')
        // console.log(id);
    $.ajax({
        type: 'get',
        url: 'http://localhost:8080/api/v1/index/rank',
        success: (res) => {
            // console.log(res);
            let rank = template('rank', res)
            $('.common_wrap>.content_list').eq(0).html(rank)
        },
    })

    function latest_comment() {
        $.ajax({
            type: 'get',
            url: 'http://localhost:8080/api/v1/index/latest_comment',
            success: (res) => {
                let date = new Date()
                res.time = date.toLocaleDateString().split('/')
                let comment = template('comment', res)
                    // console.log(comment);
                $('.common_wrap>.content_list').eq(1).html(comment)
            },
        })
    }

    latest_comment();

    $.ajax({
        type: 'get',
        url: 'http://localhost:8080/api/v1/index/attention',
        success: (res) => {
            let date = new Date()
            res.time = date.toLocaleDateString().split('/')
            console.log(res);
            let list = template('list', res)
                // console.log(comment);
            $('.common_wrap>.content_list').eq(2).html(list)
        },
    })
    $.ajax({
        type: 'get',
        url: 'http://localhost:8080/api/v1/index/article',
        data: { id },
        success: (res) => {
            console.log(res);
            $('.article_title').text(res.data.title)
            $('.article_info').text(`${res.data.author} 发布于 ${res.data.date} 07:08:46   分类: ${res.data.category}   阅读:
                (${res.data.read})   评论: (${res.data.comments})`)
            $('.article_con').html(res.data.content)
            if (res.data.next != null) {
                $('.article_links>a').eq(0).text(res.data.next.title).attr('href', `./article.html?id=${res.data.next.id}`)
            }
            if (res.data.prev != null) {
                $('.article_links>a').eq(1).text(res.data.prev.title).attr('href', `./article.html?id=${res.data.prev.id}`)
            }
        },
    })
    $.ajax({
        type: 'get',
        url: 'http://localhost:8080/api/v1/index/get_comment',
        data: { articleId: id },
        success: (res) => {
            let date = new Date()
            res.time = date.getHours()
            console.log(res);
            let comment_this = template('comment_this', res)
            $('.comment_list_con').html(comment_this)
            $('.comment_count').html(res.data.length + '条评论')
        },
    })

    $('.comment_sub').on('click', function(e) {
        e.preventDefault()
        let name = $('.comment_name').val().trim()
        let comment = $('.comment_input').val().trim()
        if (name == '' && comment == '') return;
        $.ajax({
            type: 'post',
            url: 'http://localhost:8080/api/v1/index/post_comment',
            data: {
                author: name,
                content: comment,
                articleId: id,
            },
            success: (res) => {
                $('.comment_name').val('')
                $('.comment_input').val('')
                layer.msg(res.msg)
                console.log(res);
                latest_comment()
            },
        })

    });



    let searchObj = {
        key: '',
        type: '',
        page: '',
        perpage: ''
    }


    function getSearch() {
        $.ajax({
            type: 'get',
            url: 'http://localhost:8080/api/v1/index/search',
            data: searchObj,
            success: (backData) => {
                console.log(backData)
            },
        })
    }
    getSearch();

    $('.search_btn').on('click', function() {
        let keyText = $('.search_txt').val().trim();
        searchObj.key = keyText;
        console.log(searchObj.key);
        window.location.href = './list.html?key=' + searchObj.key
    })

})